package cn.itcast.controller;

import cn.itcast.entity.MessageConstant;
import cn.itcast.entity.RedisMessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/send4Order.do")
    public Result send4Order(String telephone) {
        //获取验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);

        try {
            //将验证码发向手机
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            System.out.println("==========================");
            System.out.println("手机验证码发送成功！"+code);
            System.out.println("==========================");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //向验证码保存到redis中用于数据比对
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER,300,code.toString());


        return new Result(false, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }


    @RequestMapping("/send4Login.do")
    public Result send4Login(String telephone) {
        //获取验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);

        try {
            //将验证码发向手机
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,code.toString());
            System.out.println("==========================");
            System.out.println("手机验证码发送成功！"+code);
            System.out.println("==========================");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //向验证码保存到redis中用于数据比对
        jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN,300,code.toString());


        return new Result(false, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }





}
